const app = getApp()
Page({
  data: {
    isbind: false,
    showbottom: false,//显示到底部
    isBottom: false,//是否加载
    loadingHidden: false,
    focus: false,
    user_type:'',
    messagedata :{},//留言数据
    uid:'',
    message:'',
    lasttime:'',//最后一条记录时间
    userInfo: {},//微信账号数据
    hasUserInfo: false,
  },
  bindButtonTap() {
    this.setData({
      focus: true
    })
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "留言"
    })
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      //授权成功直接获取用户信息
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    if (getApp().globalData.bindInfo) {
      that.getmessagelist("");//获取留言列表,家长才可以留言
      this.setData({
        uid: app.globalData.bindInfo.user_id,
        user_type: getApp().globalData.bindInfo.user_type
      })
      // that.setData({
      //   loadingHidden: true,
      // });
      app.isbindBack = function (result) {
        that.setData({
          loadingHidden: true,
        });
        if (result.code == 1) {
          wx.redirectTo({
            url: '../index/index'
          })
          getApp().globalData.bindInfo = result.data;
          that.getmessagelist("");//获取留言列表,家长才可以留言
          that.setData({
            uid: app.globalData.bindInfo.user_id,
            user_type: app.globalData.bindInfo.user_type
          });
        } else if (result.code == 0) {
          wx.redirectTo({
            url: '../binding/binding'
          })
        }
      }
    }
  },
  onReady: function () {
  },
  //监听页面刷新数据
  onShow: function () {
    var that = this;
    that.setime();
    var type = "";
    var avatarUrl = "";
    if (app.globalData.bindInfo) {
      if (app.globalData.bindInfo.user_type) {
        that.setData({
          user_type: app.globalData.bindInfo.user_type,
          uid: app.globalData.bindInfo.user_id
        })
      }
      var messagedata = that.data.messagedata;//留言数据
      if (!messagedata){
        if (app.globalData.bindInfo.user_type==3){
          that.getmessagelist("");//获取留言列表,家长才可以留言
        }
      }
    } else {
      wx.redirectTo({
        url: '../binding/binding'
      })
    }

  },
  setime: function (options) {
    var that = this;
    var timer; 
    timer = setTimeout(function () {
      that.getmessagelist();
    }, 8000);
  },

  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
    var data = this.data.messagedata;//留言数据
    var lasttime="";
    var num=0;
    if(data.length >0){
      num = num+1;
      lasttime = data[num]['create_time'];
    }
    var isBottom = this.data.isBottom;
    if (!isBottom) {
      this.setData({ showbottom: true });
      return;
    }
    var that = this;
    that.getmessagelist(lasttime);
  },

  getNowTime: function () {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    //  如果需要时分秒，就放开
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    var formatDate = year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
    return formatDate;
  },

  onShareAppMessage: function () {
  },
  //获取消息数据
  getmessagelist: function (options) {
    var lasttime = options;
    if (options == "" || options==null){
      var now_time = this.getNowTime();
      lasttime = now_time;
    }
    var that = this;
    var openid = app.globalData.openid;
    var link = app.globalData.schlink;
    var uid=0;
    var sign='';
    if (app.globalData.bindInfo){
      var uid = app.globalData.bindInfo.user_id;
      var sign = app.globalData.bindInfo.sign;
    }
    var dateline='';
    if (lasttime){
      dateline = lasttime;
    }
    if (uid == 0 || uid == null || uid==undefined){
      return;
    }
    var compare = 0;
    wx.request({
      url: getApp().globalData.baseUrl + 'msglist',
      data: {
        link: link,
        openid: openid,
        sign: sign,
        uid:uid,
        dateline: dateline,
        compare: compare
      },
      method: "get",
      header: {},
      success: function (res) {
        var messagedata = res.data.data.data;
        if (res.data.code == 200) {
          if (messagedata.length > 0) {
            that.setData({ messagedata: messagedata});
          } else {
            that.setData({ messagedata: false });//没有数据
          }
        }else{
          that.setData({ messagedata: false });//没有数据
        }
        that.setData({ loadingHidden: true });
      }
    })
  },
  //提交留言
  bindButtonTap: function (e) {
    var that = this;
    var message = e.detail.value;
    that.setData({ message: message });
    if (message == "" || message == null || message==undefined){
      wx.showToast({
        title: '留言不为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    var openid = app.globalData.openid;
    var link = app.globalData.schlink;
    var uid = app.globalData.bindInfo.user_id;
    var sign = app.globalData.bindInfo.sign;
    wx.request({
      url: getApp().globalData.baseUrl + 'send',
      data: {
        link: link,
        openid: openid,
        sign: sign,
        uid: uid,
        message: message,
      },
      method: "post",
      header: {},
      success: function (res) {
        wx.showToast({
          title: app.globalData.bindInfo.user_type,
          icon: 'none',
          duration: 1500
        })
        that.getmessagelist();//刷新数据
        that.setData({message: '' });
        if (res.data.code == 200) {
          if (res.data.data.code==1){
            wx.showToast({
              title: '留言成功',
              icon: 'succes',
              duration: 1500,
              mask: true
            })
          }else{
            wx.showToast({
              title: '留言失败',
              icon: 'none',
              duration: 1500
            })
          }
        } else {
          wx.showToast({
            title: '留言失败',
            icon: 'none',
            duration: 1500
          })
        }
        // that.getmessagelist();//刷新数据
      }
    })

  },

})